package com.authManager.server.controller;

import com.authManager.entity.SysCode;
import com.authManager.server.service.ISysCodeService;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 【系统编码表】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-19
 */

@Api(value = "/admin/sysCode", tags = "【系统编码表】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysCode", produces = BaseController.defProduces)
public class SysCodeController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysCodeController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysCodeService sysCodeService;

	@ApiOperation(value = "添加【系统编码表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "codeType", value = "编码类型", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "code", value = "编码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "编码名称", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "val", value = "编码值", required = false), })
	@RequestMapping(value = "saveSysCode", method = { RequestMethod.POST })
	public ResultData<Object> saveSysCode(@RequestParam(value = "",required = false) String codeType,
			@RequestParam(required = false) String code, @RequestParam(required = false) String name,
			@RequestParam(required = false) String val) {
		SysCode sysCode = new SysCode();
		if (StringUtils.isNotBlank(codeType)) {
			sysCode.setCodeType(codeType);
		}
		if (StringUtils.isNotBlank(code)) {
			sysCode.setCode(code);
		}
		if (StringUtils.isNotBlank(name)) {
			sysCode.setName(name);
		}
		if (StringUtils.isNotBlank(val)) {
			sysCode.setVal(val);
		}
		sysCodeService.insert(sysCode);
		Object sysCodeId = sysCode.getSysCodeId();
		return new ResultData.Builder<Object>(ResultCode.success).setData(sysCodeId).build();
	}

	@ApiOperation(value = "修改【系统编码表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "sysCodeIdStr", value = "主键", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "codeType", value = "编码类型", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "code", value = "编码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "编码名称", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "val", value = "编码值", required = false), })
	@RequestMapping(value = "editSysCode", method = { RequestMethod.POST })
	public ResultData<Object> editSysCode(@RequestParam(required = true) Integer sysCodeId,
			@RequestParam(required = false) String codeType, @RequestParam(required = false) String code,
			@RequestParam(required = false) String name, @RequestParam(required = false) String val) {
		SysCode sysCode = sysCodeService.selectById(sysCodeId);
		if (sysCode == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysCode = new SysCode();
		if (sysCodeId != null) {
			sysCode.setSysCodeId(sysCodeId);
		}
		if (StringUtils.isNotBlank(codeType)) {
			sysCode.setCodeType(codeType);
		}
		if (StringUtils.isNotBlank(code)) {
			sysCode.setCode(code);
		}
		if (StringUtils.isNotBlank(name)) {
			sysCode.setName(name);
		}
		if (StringUtils.isNotBlank(val)) {
			sysCode.setVal(val);
		}
		sysCodeService.updateById(sysCode);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【系统编码表】")
	@RequestMapping(value = "delSysCode", method = { RequestMethod.POST })
	public ResultData<Object> delSysCode(@RequestParam(required = true) Long sysCodeId) {
		SysCode sysCode = sysCodeService.selectById(sysCodeId);
		if (sysCode == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		// sysCodeService.deleteById(sysCodeId);
		// 逻辑删除
		// sysCode.setStatus(0);
		// sysCodeService.updateById(sysCodeId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【系统编码表】")
	@RequestMapping(value = "getSysCode", method = { RequestMethod.POST })
	public ResultData<SysCode> getSysCode(@RequestParam(required = true) Long sysCodeId) {
		SysCode sysCode = sysCodeService.selectById(sysCodeId);
		if (sysCode == null) {
			return new ResultData.Builder<SysCode>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysCode>(ResultCode.success).setData(sysCode).build();
	}

	@ApiOperation(value = "列表【系统编码表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "sysCodeId", value = "主键", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "codeType", value = "编码类型", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "code", value = "编码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "编码名称", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "val", value = "编码值", required = false), })
	@RequestMapping(value = "listSysCode", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysCode>> listSysCode(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Integer sysCodeId, @RequestParam(required = false) String codeType,
			@RequestParam(required = false) String code, @RequestParam(required = false) String name,
			@RequestParam(required = false) String val) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		// 主键
		if (sysCodeId != null) {
			paramMap.put("sysCodeId", sysCodeId);
		}
		// 编码类型
		if (StringUtils.isNotBlank(codeType)) {
			paramMap.put("codeType", codeType);
		}
		// 编码
		if (StringUtils.isNotBlank(code)) {
			paramMap.put("code", code);
		}
		// 编码名称
		if (StringUtils.isNotBlank(name)) {
			paramMap.put("name", name);
		}
		// 编码值
		if (StringUtils.isNotBlank(val)) {
			paramMap.put("val", val);
		}
		return sysCodeService.listSysCode(paramMap);
	}
}
